//
//  NSString+WKAdd.m
//  Pods-SEGWebView_Example
//
//  Created by 山刘 on 2020/3/9.
//

#import "NSString+WKAdd.h"

@implementation NSString (WKAdd)
- (NSString *)wkStringByURLDecode {
    if ([self respondsToSelector:@selector(stringByRemovingPercentEncoding)]) {
        return [self stringByRemovingPercentEncoding];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        CFStringEncoding en = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
        NSString *decoded = [self stringByReplacingOccurrencesOfString:@"+"
                                                            withString:@" "];
        decoded = (__bridge_transfer NSString *)
        CFURLCreateStringByReplacingPercentEscapesUsingEncoding(
                                                                NULL,
                                                                (__bridge CFStringRef)decoded,
                                                                CFSTR(""),
                                                                en);
        return decoded;
#pragma clang diagnostic pop
    }
}

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

-(NSDictionary *)toDictionary {
    if (self == nil) {
        return nil;
    }

    NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

+ (NSString*)toString:(id)objStr
{
    if ([objStr isEqual:nil]) {
        return [NSString string];
    }
    
    if ([objStr isEqual:[NSNull null]]) {
        return [NSString string];
    }
    
    if ([objStr isKindOfClass:[NSString class]]) {
        return objStr;
    }
    
    if ([objStr isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@", objStr];
    }
    
    return [NSString string];
}

+(NSString*)resourceName:(NSString*)name{
    if (!name || name.length <= 0) return @"";
    
    NSString *path = @"";
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"SEGWebView" withExtension:@"bundle"];
    if (url) {
        path = [[[NSBundle bundleWithURL:url] resourcePath] stringByAppendingPathComponent:name];
    }
    return path;
}
@end
